﻿using System;
using System.Text;
using System.ServiceModel;
using System.ComponentModel;
using Sinacor.Infra.UI.Common;
using System.Collections.Generic;
using Sinacor.Infra.UI.Security.Services;
using Sinacor.UI.Bovespa.Corretagem.Communication.Bovespa.Corretagem.Taxa;

namespace Sinacor.UI.Bovespa.Corretagem.Communication
{
    public class Taxa : BaseCommunication
    {
         #region Construtor

        public Taxa(SessionInformation sessionInformation)
            : base(sessionInformation)
        {
        }

        #endregion

        #region AlterarTaxa

        public void AlterarTaxa(Model.Taxa request)
        {
            ITaxa servico = null;
            try
            {
                using (ServiceInstance<ITaxa> service = new ServiceInstance<ITaxa>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.AlterarTaxa(new Translator.ModelToDataContract.Taxa().ToTaxaRequest(request));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region ConsultarTaxas

        public ModelCollection<Model.Taxa> ConsultarTaxas(Model.Parameter.FiltroTaxa request)
        {
            ITaxa servico = null;
            ModelCollection<Model.Taxa> retorno = null;
            try
            {
                using (ServiceInstance<ITaxa> service = new ServiceInstance<ITaxa>())
                {
                    servico = service.TryGetInstance(this.Session);

                    List<TaxaResponse> taxaresponse = servico.ConsultarTaxas(new Translator.ModelToDataContract.Taxa().ToFiltroTaxaRequest(request));

                    retorno = new Translator.DataContractToModel.Taxa().ToTaxa(taxaresponse);
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }
            return retorno;
        }

        #endregion

        #region ExcluirTaxa

        public void ExcluirTaxa(Model.ChaveTaxa request)
        {
            ITaxa servico = null;
            try
            {
                using (ServiceInstance<ITaxa> service = new ServiceInstance<ITaxa>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.ExcluirTaxa(new Translator.ModelToDataContract.Taxa().ToChaveTaxaRequest(request));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

        #region IncluirTaxa

        public void IncluirTaxa(Model.Taxa request)
        {
            ITaxa servico = null;
            try
            {
                using (ServiceInstance<ITaxa> service = new ServiceInstance<ITaxa>())
                {
                    servico = service.TryGetInstance(this.Session);
                    servico.IncluirTaxa(new Translator.ModelToDataContract.Taxa().ToTaxaRequest(request));
                }
            }
            catch (Exception ex)
            {
                HandleFault(ex);
            }

        }

        #endregion

    }
}
